{% if ingress_type|lower == "ingress" %}
---
{% if ingress_api_version is defined %}
apiVersion: '{{ ingress_api_version }}'
{% endif %}
kind: Ingress
metadata:
  name: '{{ ansible_operator_meta.name }}-ingress'
  namespace: '{{ ansible_operator_meta.namespace }}'
  labels:
    {{ lookup("template", "../common/templates/labels/common.yaml.j2") | indent(width=4) | trim }}
{% if ingress_annotations or ingress_controller|lower == "contour" %}
  annotations:
{% if ingress_annotations %}
    {{ ingress_annotations | indent(width=4) }}
{% endif %}
{% if ingress_controller|lower == "contour" %}
    projectcontour.io/websocket-routes: "/websocket"
    kubernetes.io/ingress.class: contour
{% endif %}
{% endif %}
spec:
{% if ingress_class_name %}
  ingressClassName: '{{ ingress_class_name }}'
{% endif %}
  rules:
    - http:
        paths:
          - path: '{{ ingress_path }}'
            pathType: '{{ ingress_path_type }}'
            backend:
              service:
                name: '{{ ansible_operator_meta.name }}-service'
                port:
                  number: 80
{% if ingress_controller|lower == "contour" %}
          - path: '{{ ingress_path.rstrip("/") }}/websocket'
            pathType: '{{ ingress_path_type }}'
            backend:
              service:
                name: '{{ ansible_operator_meta.name }}-service'
                port:
                  number: 80
{% endif %}
{% if hostname %}      
      host: {{ hostname }}
{% endif %}      
{% if ingress_tls_secret %}
  tls:
    - hosts:
        - {{ hostname }}
      secretName: {{ ingress_tls_secret }}
{% endif %}
{% endif %}

{% if ingress_type|lower == "route" %}
---
{% if route_api_version is defined %}
apiVersion: '{{ route_api_version }}'
{% endif %}
kind: Route
metadata:
  name: '{{ ansible_operator_meta.name }}'
  namespace: '{{ ansible_operator_meta.namespace }}'
  labels:
    {{ lookup("template", "../common/templates/labels/common.yaml.j2") | indent(width=4) | trim }}
spec:
{% if route_host != '' %}
  host: {{ route_host }}
{% endif %}
  port:
    targetPort: '{{ (route_tls_termination_mechanism | lower == "passthrough") | ternary("https", "http") }}'
  tls:
    insecureEdgeTerminationPolicy: Redirect
    termination: {{ route_tls_termination_mechanism | lower }}
{% if route_tls_termination_mechanism | lower == 'edge' and route_tls_secret != '' %}
    key: |-
{{ route_tls_key | indent(width=6, first=True) }}
    certificate: |-
{{ route_tls_crt | indent(width=6, first=True) }}
{% if route_ca_crt is defined %}
    caCertificate: |-
{{ route_ca_crt | indent(width=6, first=True) }}
{% endif %}
{% endif %}
  to:
    kind: Service
    name: {{ ansible_operator_meta.name }}-service
    weight: 100
  wildcardPolicy: None
{% endif %}
